<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true">
      <el-form-item label="软件名称" prop="softwareName">
        <el-input
          v-model="queryParams.softwareName"
          placeholder="请输入软件名称"
          clearable
          style="width: 100%"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="软件制作时间" prop="softwareCreateTime">
        <el-date-picker
          v-model="softwareCreateQueryTime"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          placeholder="请输入软件制作时间"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 100%"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="软件制作人" prop="softwareCreater">
        <el-input
          v-model="queryParams.softwareCreater"
          placeholder="请输入软件制作人"
          clearable
          style="width: 100%"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="软件类型" prop="softwareType">
        <el-select
          v-model="queryParams.softwareType"
          style="width: 80%"
          clearable
          placeholder="请选择软件类型"
        >
          <el-option
            v-for="item in softwareTypeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="small"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:user:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>

      <!-- <right-toolbar :show-search.sync="showSearch"
                     :columns="columns"
                     @queryTable="getList" /> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="softwareList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="软件名称"
        align="center"
        prop="softwareName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="软件制作时间"
        align="center"
        prop="softwareCreateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.softwareCreateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="软件制作人"
        align="center"
        prop="softwareCreater"
        width="180"
      />
      <el-table-column
        label="版本"
        align="center"
        prop="softwareVersion"
        width="180"
      />
      <el-table-column
        label="软件类型"
        align="center"
        prop="softwareType"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.softwareType == "0" ? "专用" : "基础" }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="自动化部署"
        align="center"
        prop="auto"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.auto == "1" ? "是" : "否" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:dict:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            v-hasPermi="['system:dict:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            v-hasPermi="['system:dict:remove']"
            size="mini"
            type="text"
            icon="el-icon-reading"
            @click="handleHistory(scope.row)"
            >历史</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <model-detail ref="modelDetail" @ok="getList"></model-detail>
    <!-- 删除二次确认弹窗 -->
    <el-dialog
      title="提示"
      :visible.sync="delConfirmVisible"
      :close-on-click-modal="false"
      width="30%"
    >
      <el-alert
        title="是否全部删除还是回退上个版本？"
        type="warning"
        show-icon
        :closable="false"
      ></el-alert>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setCancle">全部删除</el-button>
        <el-button type="primary" @click="setConfirm">回退</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listSoftwate, delSoftware } from "@/api/resource/modelSoftware.js";
import {
  deleteAllHistory,
  rollbackHistory,
} from "@/api/resource/software-history.js";
import ModelDetail from "./components/detail.vue";
export default {
  components: { ModelDetail },
  data() {
    return {
      modelSoftwareId: void 0,
      delConfirmVisible: false,
      // 遮罩层
      loading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        softwareName: void 0,
        softwareCreater: void 0,
        softwareType: "",
      },
      //软件类型
      softwareTypeList: [
        { value: "0", label: "专用软件" },
        { value: "1", label: "基础软件" },
      ],
      // 表格数据
      softwareList: [],
      // 时间查询list
      softwareCreateQueryTime: void 0,
      total: 0,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询模型软件列表 */
    getList() {
      this.loading = true;
      listSoftwate(
        this.addDateRange(
          this.queryParams,
          this.softwareCreateQueryTime,
          "SoftwareCreateTime"
        )
      ).then((response) => {
        this.softwareList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.softwareCreateQueryTime = [];
      (this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        softwareName: void 0,
        softwareCreater: void 0,
      }),
        this.handleQuery();
    },
    /** 新增模型软件操作 */
    handleAdd() {
      this.$refs.modelDetail.addOpen();
    },
    setConfirm() {
      rollbackHistory(this.modelSoftwareId).then((res) => {
        if (res.code == 200) {
          this.handleQuery();
          this.$modal.msgSuccess("回退成功");
          this.delConfirmVisible = false;
        }
      });
    },
    setCancle() {
      delSoftware(this.modelSoftwareId).then((res) => {
        if (res.code == 200) {
          this.handleQuery();
          this.$modal.msgSuccess("删除成功");
          this.delConfirmVisible = false;
        }
      });
    },
    /** 删除模型软件 */
    handleDelete(row) {
      this.modelSoftwareId = row.softwareId;
      this.delConfirmVisible = true;
    },
    /** 修改模型软件操作 */
    handleUpdate(row) {
      this.$refs.modelDetail.editOpen(row);
    },
    handleHistory(row) {
      this.$router.push("/resource/software/history/" + row.softwareId || 0);
    },
  },
};
</script>

<style lang="scss" scoped>
.el-alert--warning.is-light {
  background-color: white;
  color: rgb(0, 0, 0);
}
::v-deep .el-alert__icon {
  color: #e6a23c;
  font-size: 18px;
}
::v-deep .el-alert__title {
  font-size: 15px;
}
</style>
